/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.figures;

import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ViewportAwareConnectionLayerClippingStrategy;
import org.eclipse.draw2d.ViewportUtilities;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.fireflow.pdl.fpdl20.diagram.LaneShape;
import org.fireflow.pdl.fpdl20.diagram.style.BoundsStyle;
import org.fireflow2.designer.eclipse.figures.widget.PoolLaneBorder;
import org.fireflow2.designer.eclipse.modelwrapper.basic.LaneWrapper;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class LaneFigure extends RectangleFigure implements CommonFigureInterface{
	private LaneWrapper laneWrapper = null;
	private PoolLaneBorder laneBorder = null;
	private Color borderColor = null;
	private boolean isHorizonal = true;
	
	public LaneFigure(LaneWrapper poolWrapper){
		this.laneWrapper = poolWrapper;
		this.setLayoutManager(new FreeformLayout());
		
		laneBorder = new PoolLaneBorder(1);
		laneBorder.setTextAlignment(PositionConstants.CENTER);
		laneBorder.setTextColor(ColorConstants.black);
		laneBorder.setPadding(new Insets(3,10,3,10));
		this.setBorder(laneBorder);
	}
	
	public Rectangle getBox(){
		Rectangle rectangle = new Rectangle(this.getBounds());
		return rectangle;
	}
	protected boolean useLocalCoordinates() {
		return true;
	}
	public Rectangle getSelectionHandleBounds(){
		Rectangle rectangle = new Rectangle(this.getBounds());
		this.translateToAbsolute(rectangle);
		rectangle.translate(CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE);
		rectangle.resize(CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE);
		return rectangle;
	}
	public void refreshFigure(){
		
		LaneShape laneShape = (LaneShape)laneWrapper.getDiagramElement();
		org.fireflow.pdl.fpdl20.diagram.basic.Rectangle fireRect = 
			(org.fireflow.pdl.fpdl20.diagram.basic.Rectangle)laneShape.getShape();
		
		//宽度自动从Pool的ClientArea获取
		this.setPreferredSize(fireRect.getBounds().getWidth(),fireRect.getBounds().getHeight());
		
		
		laneBorder.setLabel(fireRect.getTitle());
		
		//设置边框颜色、线型
		String color = fireRect.getBounds().getBoundsStyle().getColor();
		if (color==null || color.trim().equals("")){
			color = "#696969";
		}
		borderColor = Util4Eclipse.colorStr2ColorObj(color);
		laneBorder.setBorderColor(borderColor);
		
		BoundsStyle boundsStyle = fireRect.getBounds().getBoundsStyle();
		this.setLineWidth(boundsStyle.getThick());

		
		
		//设置字体
	    Color fontColor = Util4Eclipse.colorStr2ColorObj(fireRect.getTitleLabel().getFont().getColor());    
	    String fontName = "Trebuchet MS";
	    Font systemFont = Display.getCurrent().getSystemFont();
	    if (systemFont!=null && systemFont.getFontData().length>0){
	    	FontData fd = systemFont.getFontData()[0];
	    	if (fd.getName()!=null){
	    		fontName = fd.getName();
	    	}		    	
	    }
	    int fontStyle = SWT.NORMAL;
	    if (org.fireflow.pdl.fpdl20.diagram.style.Font.FONT_WEIGHT_BOLD.equals(fireRect.getTitleLabel().getFont().getWeight())){
	    	fontStyle = SWT.BOLD;
	    }
	    Font newFont = new Font(Display.getCurrent(),fontName,fireRect.getTitleLabel().getFont().getSize(),fontStyle);
	    laneBorder.setTextColor(fontColor);
	    laneBorder.setFont(newFont);

	    this.repaint();
	}


	protected void outlineShape(Graphics graphics){
		Color oldColor = graphics.getForegroundColor();
		graphics.setForegroundColor(borderColor);
		super.outlineShape(graphics);
		graphics.setForegroundColor(oldColor);
	}	
}
